package com.qiaofang.collectmanagebff.facade.impl;

import com.qiaofang.collectmanage.stub.bean.*;
import com.qiaofang.collectmanage.stub.constants.CollectRuleResponseCode;
import com.qiaofang.collectmanagebff.facade.request.*;
import com.qiaofang.collectmanagebff.facade.response.*;
import com.qiaofang.collectmanagebff.service.HouseAgentService;
import com.qiaofang.common.constant.CommonResponseCode;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.page.PageDTO;
import com.qiaofang.common.util.ConverterBeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/houseagent")
@Api(value = "中介数据集合", tags = "houseagent")
public class HouseAgentFacadeImplBff {
    @Autowired
    private HouseAgentService houseAgentService;

    @RequestMapping(value = "/saveHouseAgent", method = RequestMethod.POST)
    @ApiOperation(value = "保存中介信息", notes = "保存中介信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "SaveHouseAgentBffRequest")})
    public SaveCollectRuleBffResponse saveHouseAgent(@RequestBody @Valid SaveHouseAgentBffRequest request, BindingResult bindingResult) {
        SaveCollectRuleBffResponse response = new SaveCollectRuleBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
        SaveHouseAgentDTO saveHouseAgentDTO = new SaveHouseAgentDTO();
        ConverterBeanUtil.copyBeanProperties(request, saveHouseAgentDTO);
        houseAgentService.saveHouseAgent(saveHouseAgentDTO);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
        response.setData(true);
        return response;
    }

    @RequestMapping(value = "/removeHouseAgent", method = RequestMethod.POST)
    @ApiOperation(value = "删除中介信息", notes = "删除中介信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "RemoveHouseAgentBffRequest")})
    public RemoveHouseAgentBffResponse removeHouseAgent(@RequestBody @Valid RemoveHouseAgentBffRequest request, BindingResult bindingResult) {
        RemoveHouseAgentBffResponse response = new RemoveHouseAgentBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
        RemoveHouseAgentDTO removeHouseAgentDTO = new RemoveHouseAgentDTO();
        ConverterBeanUtil.copyBeanProperties(request, removeHouseAgentDTO);
        houseAgentService.removeHouseAgent(removeHouseAgentDTO);
        if (!CommonResponseCode.RC_SUCCESS.getResponseCode().equals(response.getResponseCode())) {
            throw new BusinessException(response.getResponseCode(), response.getResponseMessage());
        }
        response.setData(true);
        return response;
    }


    @RequestMapping(value = "/searchHouseAgentPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询多个对象不包含删的分页列表", notes = "获取多个对象不包含删的分页列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "request", value = "请求参数", dataType = "SearchHouseAgentPageBffRequest")})
    public SearchHouseAgentPageBffResponse searchHouseAgentPage(@RequestBody @Valid SearchHouseAgentPageBffRequest request, BindingResult bindingResult) {
        SearchHouseAgentPageBffResponse response = new SearchHouseAgentPageBffResponse();
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            response.setResponseCode(CollectRuleResponseCode.RC_PARAMETER_ERROR.getResponseCode());
            response.setResponseMessage(defaultMessage);
            return response;
        }
        PageDTO pageDTO = new PageDTO();
        SearchHouseAgentPostDTO searchHouseAgentPostDTO = new SearchHouseAgentPostDTO();
        pageDTO.setPageSize(request.getPageSize());
        pageDTO.setPageNum(request.getPageNum());
        searchHouseAgentPostDTO.setAgentTelephone(request.getAgentTelephone());
        searchHouseAgentPostDTO.setAgentUserName(request.getAgentUserName());
        response.setData(houseAgentService.searchHouseAgentPage(pageDTO, searchHouseAgentPostDTO));
        response.setResponseCode(CommonResponseCode.RC_SUCCESS.getResponseCode());
        response.setResponseMessage(CommonResponseCode.RC_SUCCESS.getResponseMessage());
        return response;
    }
}

